package com.ruoyi.web.controller.card;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.card.domain.CardOpenRecord;
import com.ruoyi.card.service.ICardOpenRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 开卡记录Controller
 *
 * @author ancyshi
 * @date 2023-06-05
 */
@Controller
@RequestMapping("/card/openRecord")
public class CardOpenRecordController extends BaseController {
    private String prefix = "card/openRecord";

    @Autowired
    private ICardOpenRecordService cardOpenRecordService;

    @RequiresPermissions("card:openRecord:view")
    @GetMapping()
    public String openRecord() {
        return prefix + "/openRecord";
    }

    /**
     * 查询开卡记录列表
     */
    @RequiresPermissions("card:openRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CardOpenRecord cardOpenRecord) {
        startPage();
        Long userId = ShiroUtils.getUserId();
        if (!SysUser.isAdmin(userId)) {
            cardOpenRecord.setUserId(userId);
        }
        List<CardOpenRecord> list = cardOpenRecordService.selectCardOpenRecordList(cardOpenRecord);
        return getDataTable(list);
    }

    /**
     * 导出开卡记录列表
     */
    @RequiresPermissions("card:openRecord:export")
    @Log(title = "开卡记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CardOpenRecord cardOpenRecord) {
        Long userId = ShiroUtils.getUserId();
        if (!SysUser.isAdmin(userId)) {
            cardOpenRecord.setUserId(userId);
        }
        List<CardOpenRecord> list = cardOpenRecordService.selectCardOpenRecordList(cardOpenRecord);
        ExcelUtil<CardOpenRecord> util = new ExcelUtil<CardOpenRecord>(CardOpenRecord.class);
        return util.exportExcel(list, "openRecord");
    }

    /**
     * 新增开卡记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存开卡记录
     */
    @RequiresPermissions("card:openRecord:add")
    @Log(title = "开卡记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CardOpenRecord cardOpenRecord) {
        Long userId = ShiroUtils.getUserId();
        if (userId != null) {
            cardOpenRecord.setUserId(userId);
        }
        return toAjax(cardOpenRecordService.insertCardOpenRecord(cardOpenRecord));
    }

    /**
     * 修改开卡记录
     */
    @GetMapping("/edit/{openRecordId}")
    public String edit(@PathVariable("openRecordId") Long openRecordId, ModelMap mmap) {
        CardOpenRecord cardOpenRecord = cardOpenRecordService.selectCardOpenRecordById(openRecordId);
        mmap.put("cardOpenRecord", cardOpenRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存开卡记录
     */
    @RequiresPermissions("card:openRecord:edit")
    @Log(title = "开卡记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CardOpenRecord cardOpenRecord) {
        return toAjax(cardOpenRecordService.updateCardOpenRecord(cardOpenRecord));
    }

    /**
     * 删除开卡记录
     */
    @RequiresPermissions("card:openRecord:remove")
    @Log(title = "开卡记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(cardOpenRecordService.deleteCardOpenRecordByIds(ids));
    }
}
